# -*- coding:utf-8 -*-

__author__ = "Jim"

"""
Create by Jim on2018/12/30
"""

from flask import Flask
from flask_session import Session
from flask_wtf import CSRFProtect
from app.config import env
from redis import StrictRedis
from flask_sqlalchemy import SQLAlchemy

redis_store = None
db = SQLAlchemy()

from . import models


def create_app(pattern):
    app = Flask(__name__)
    Session(app)
    # CSRFProtect(app)
    config = env.get(pattern)
    app.config.from_object(config)
    global redis_store
    redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True)
    # 注册首页蓝图
    from app.modules.index import index_blueprint
    app.register_blueprint(index_blueprint)
    # 注册认证蓝图
    from app.modules.passport import passport_blueprint
    app.register_blueprint(passport_blueprint, url_prefix='/passport')
    # 注册用户蓝图
    from app.modules.user import user
    app.register_blueprint(user, url_prefix='/user')
    db.init_app(app)
    return app
